/**
 * 
 */
package com.vector.ding.web.service;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.dao.mybatis.mapper.GqtzgsBaseInfoMapper;
import com.vector.ding.dao.mybatis.mapper.GqtzgsBizInfoMapper;
import com.vector.ding.dao.mybatis.model.GqtzgsBaseInfo;
import com.vector.ding.dao.mybatis.model.GqtzgsBaseInfoExample;
import com.vector.ding.dao.mybatis.model.GqtzgsBizInfo;
import com.vector.ding.dao.mybatis.model.GqtzgsBizInfoExample;

/**
 * @author vector
 *
 */
@Service
public class GqtzgsMgrService {

	@Autowired
	private GqtzgsBaseInfoMapper baseMapper;
	
	@Autowired
	private GqtzgsBizInfoMapper bizMapper;
	
	public List<GqtzgsBaseInfo> getBaseInfo(int pageIndex, int pageSize, 
			HttpServletRequest request) {
		PageHelper.startPage(pageIndex, pageSize, true);
		GqtzgsBaseInfoExample example = parseBaseInfoExample(request);
		//example.setOrderByClause("number asc");
		return baseMapper.selectByExample(example);
	}
	
	private GqtzgsBaseInfoExample parseBaseInfoExample(HttpServletRequest request) {
		GqtzgsBaseInfoExample example = new GqtzgsBaseInfoExample();
		String companyName = StringUtil.getString(request.getParameter("company_name"));
		//if (StringUtil.isNotEmpty(companyName))
		example.or().andCompanyNameLike(StringUtil.wrapString(companyName, "%", "%"));
		return example;
	}
	
	public List<GqtzgsBizInfo> getBizInfo(int pageIndex, int pageSize, 
			HttpServletRequest request) {
		PageHelper.startPage(pageIndex, pageSize, true);
		GqtzgsBizInfoExample example = parseBizInfoExample(request);
		//example.setOrderByClause("number asc");
		return bizMapper.selectByExample(example);
	}
	
	private GqtzgsBizInfoExample parseBizInfoExample(HttpServletRequest request) {
		GqtzgsBizInfoExample example = new GqtzgsBizInfoExample();
		String companyName = StringUtil.getString(request.getParameter("company_name"));
		//if (StringUtil.isNotEmpty(companyName))
		example.or().andCompanyNameLike(StringUtil.wrapString(companyName, "%", "%"));
		return example;
	}
	
	public boolean addBizInfo(GqtzgsBizInfo record) {
		if (StringUtil.isEmpty(record.getNumber()))
			record.setNumber(UUID.randomUUID().toString());
		return 1 == bizMapper.insertSelective(record);
	}
	
	public boolean updateBizInfo(GqtzgsBizInfo record) {
		if (StringUtil.isEmpty(record.getNumber()))
			return false;
		return 1 == bizMapper.updateByPrimaryKeySelective(record);
	}
	
	public void deleteBizInfo(String... nums) {
		if (nums == null)
			return ;
		
		for (String num: nums) {
			bizMapper.deleteByPrimaryKey(num);
		}
	}
	
	public GqtzgsBizInfo parseBizInfo(HttpServletRequest request) {
		GqtzgsBizInfo record = new GqtzgsBizInfo();
		record.setBelongYear(new Integer(request.getParameter("belongYear")));
		record.setCompanyName(request.getParameter("companyName"));
		record.setExitProjectGrade(new BigDecimal(request.getParameter("exitProjectGrade")));
		record.setExitProjectScale(new BigDecimal(request.getParameter("exitProjectScale")));
		record.setExternalInvestGrade(new BigDecimal(request.getParameter("externalInvestGrade")));
		record.setExternalInvestScale(new BigDecimal(request.getParameter("externalInvestScale")));
		record.setGrateOrg1Grade(new BigDecimal(request.getParameter("grateOrg1Grade")));
		record.setGrateOrg1InvestScale(new BigDecimal(request.getParameter("grateOrg1InvestScale")));
		record.setGrateOrg2Grade(new BigDecimal(request.getParameter("grateOrg2Grade")));
		record.setGrateOrg2InvestScale(new BigDecimal(request.getParameter("grateOrg2InvestScale")));
		record.setGrateOrg2Name(request.getParameter("grateOrg2Name"));
		record.setGreatOrg1Name(request.getParameter("greatOrg1Name"));
		record.setRevenue(new BigDecimal(request.getParameter("revenue")));
		record.setRevenueGrade(new BigDecimal(request.getParameter("revenueGrade")));
		if (StringUtil.isEmpty(request.getParameter("number")))
			record.setNumber(UUID.randomUUID().toString());
		else
			record.setNumber(request.getParameter("number"));
		
		return record;
	}
	
	public void importFormFile(File importFile) {
		HSSFWorkbook workBook = null;
		try {
			workBook = new HSSFWorkbook(FileUtils.openInputStream(importFile));
			HSSFSheet sheet = workBook.getSheetAt(0);
			HSSFRow titleRow = sheet.getRow(0);
			for (int cellIdx = titleRow.getFirstCellNum(); cellIdx < titleRow.getLastCellNum(); ++cellIdx) {
				System.out.println(titleRow.getCell(cellIdx).getStringCellValue());
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if (workBook != null) {
				try {
					workBook.close();
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
	}
	
	/*public GqtzgsBaseInfo getBaseInfoBean(HSSFRow row) {
		GqtzgsBaseInfo bean = new GqtzgsBaseInfo();
		bean.setNumber(row.getC);
	}*/
}
